package com.yonyou.ucf.mdf.app.isvdxq.secondpart.service;

import com.yonyou.ucf.mdf.app.isvdxq.ReturnResult.DataResult;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.entity.RfidCodebind;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.service.LocationEntityService;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.entity.EpcEntity;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.entity.StorerecordEntity;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.entity.TaglistEntity;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.mapper.impl.EpclistMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.mapper.impl.StorerecordEntityMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.mapper.impl.TaglistMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.utils.MyTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EpcotherService {

    @Autowired
    private EpclistService epclistService;
    @Autowired
    private TaglistService taglistService;
    @Autowired
    private LocationEntityService locationEntityService;

    @Autowired
    EpclistMapperImpl tempEpclistMapper;

    @Autowired
    TaglistMapperImpl tempTaglistMapper;

    @Autowired
    StorerecordEntityMapperImpl tempStorerecordEntityMapper;

    /**
     * 盘点标签是否占用
     *
     * @param list_epcEntity
     * @return
     */
    public static boolean CheckEpcUsed(List<EpcEntity> list_epcEntity) {
        boolean result = false;
        long checkNum = list_epcEntity.stream().count();
        if (checkNum > 0) {
            return true;
        }

        return result;
    }

    // Pad盘盈发签
    @Transactional(rollbackFor = Exception.class)
    public DataResult BindEpcSubmit(List<RfidCodebind> codeBindList, String tag) {
        DataResult dataResult = new DataResult(0, "操作失败");
        if (codeBindList == null || codeBindList.size() == 0) {
            dataResult = new DataResult(0, "提交的数据条数为空!");
            return dataResult;
        }

        List<String> strList = codeBindList.stream().map(it -> it.getRfid_code()).distinct().collect(Collectors.toList());
        RfidCodebind tempEntity = codeBindList.get(0);
        String tenantId = tempEntity.getTenant_id();
        String ordertype = tempEntity.getOrdertype();
        String operuserId = tempEntity.getModifier();
        String opername = tempEntity.getModifier();
        String orgId = tempEntity.getOrgId();
        String locationId = tempEntity.getLocationId();
        String huojiaID = null;
        if (!StringUtils.isEmpty(locationId)) {
            huojiaID = locationEntityService.GetHuojiaID(locationId, tenantId);
        }
        //托盘数据组装
        List<TaglistEntity> dataList = new ArrayList<>();
        List<TaglistEntity> tempList = taglistService.SelectByEpcs(strList, tenantId);
        if (!StringUtils.isEmpty(tag))
            dataList = TaglistService.CreateEntityByEpc(strList, tempList, tenantId, operuserId, opername, tag, orgId);

        String batchNum = MyTool.GenerateBatchnoDXQ(ordertype);//私有批次
        List<EpcEntity> mylist_epc = new ArrayList<>();
        List<StorerecordEntity> mylist_record = new ArrayList<>();
        for (RfidCodebind CodeBind : codeBindList) {
            CodeBind.setShelfId(huojiaID);
            EpcEntity epcEntity = EpclistService.MakeEntityForPad(CodeBind);
            epcEntity.setBatch_no(batchNum);
            mylist_epc.add(epcEntity);
            //二期方案 补录信息修改
            StorerecordEntity storerecordEntity = StorerecordService.MakeEntityForPad(CodeBind, batchNum,
                    epcEntity.getId());
            mylist_record.add(storerecordEntity);
        }


        if (dataList.size() > 0) {
            tempTaglistMapper.insertBatch(dataList);
        }
        if (mylist_epc.size() > 0 && mylist_record.size() > 0) {
            tempEpclistMapper.insertBatch(mylist_epc);
            tempStorerecordEntityMapper.insertBatch(mylist_record);
        }

        dataResult = new DataResult(1, "信息保存成功");
        dataResult.setInfo(batchNum);
        dataResult.setDataList(mylist_epc);

        return dataResult;
    }
}
